home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / BUILDCTL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-24  |  4.4 KB  |  197 lines

  1. /* make executable with the command:
  2.    DOS:
  3.         bcc -ms -DMSDOS buildctl.c \borlandc\lib\wildargs.obj
  4.    LINUX:
  5.     gcc buildctl.c -o buildctl
  6. */
  7.  
  8.  
  9. #include <stdio.h>
  10. #include "ctype.h"
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #ifdef MSDOS
  14. #include <dir.h>
  15. #include <io.h>
  16. #else
  17. #define stricmp strcasecmp
  18. #endif
  19. #define NULLFILE (FILE *) 0
  20. #define NULLCHAR (char *) 0
  21. #define    BM_READ        2
  22.  
  23. #ifndef _lint
  24. static char rcsid[] OPTIONAL = "$Id: buildctl.c,v 1.16 1997/05/24 13:04:34 root Exp root $";
  25. #endif
  26.  
  27. static char const *dirname = NOSDIR;
  28.  
  29. typedef unsigned short int16;
  30.  
  31. void main (int argc, char *argv[]);
  32. static int isarea (char *name);
  33. static void rebuild_one (char *str);
  34.  
  35. /* a mailbox entry */
  36. struct let {
  37.     long    start;
  38.     long    size;
  39.     long    bid;
  40.     int16    status;
  41. };
  42.  
  43.  
  44. /* Returns 1 if name is in the given area file, 0 otherwise */
  45. static int
  46. isarea(char *name)
  47. {
  48. char buf[100], *cp;
  49. FILE *fp;
  50.  
  51.     sprintf (buf, "%s/etc/areas.sys", dirname);
  52.     if((fp = fopen(buf,"r")) == NULLFILE)
  53.         return 0;
  54.     while(fgets(buf,sizeof(buf),fp) != NULLCHAR)     {
  55.         /* The first word on each line is all that matters */
  56.         if(isalnum(buf[0]))     { /* skip comments */
  57.                 if((cp = strchr(buf,' ')) != NULLCHAR)
  58.                 *cp = '\0';
  59.                 /*There could still be a tab before the space ! -WG7J */
  60.             if((cp = strchr(buf,'\t')) != NULLCHAR)
  61.                 *cp = '\0';
  62.                 /*This could be a line with just the area name,
  63.                  *ie terminated with 'CR/LF' - WG7J
  64.                  */
  65.                 if((cp = strchr(buf,'\n')) != NULLCHAR)
  66.                 *cp = '\0';
  67.                 if(stricmp(name,buf) == 0) {    /* found it */
  68.                 fclose(fp);
  69.                 return 1;
  70.                 }
  71.             }
  72.         }
  73.     fclose(fp);
  74.     return 0;
  75. }
  76.  
  77.  
  78. /*lint -save -e550 */
  79. void
  80. rebuild_one (char *str)
  81. {
  82. char *cp;
  83. register FILE *fp, *cfp;
  84. int nextisBID, firstIDline;
  85. char buf[256], buf2[512];
  86. int ispublic, lines;
  87. long last;
  88. struct let l;
  89.  
  90.     cp = strrchr (str, '/');
  91.     if (!cp)
  92.         cp = strrchr (str, '\\');
  93.     if (!cp)    {
  94.         sprintf (buf2, "%s/spool/mail", dirname);
  95.         sprintf (buf, "%s/control/%s", buf2, str);
  96.         cp = str;
  97.     } else    {
  98.         *cp++ = 0;            
  99.         strcpy (buf2, str);
  100.         sprintf (buf, "%s/control/%s", str, cp);
  101.     }
  102.     if (strstr (buf, ".TXT") || strstr (buf, ".txt"))
  103.         strcpy (&buf[strlen(buf) - 3], "ctl");
  104.     else
  105.         strcat (buf, ".ctl");
  106.     if((cfp = fopen(buf,"wb")) == NULLFILE)    {
  107.         printf ("Can't create control file '%s'\n", buf);
  108.         return;
  109.     }
  110.     sprintf (buf, "%s/%s", buf2, cp);
  111.  
  112.     if (!strstr (buf, ".TXT") && !strstr (buf, ".txt"))
  113.         strcat (buf, ".txt");
  114.     if ((fp = fopen (buf, "rt")) == NULLFILE)    {
  115.         printf ("Can't open file '%s'\n", buf);
  116.         return;
  117.     }
  118.     firstIDline = nextisBID = 0;
  119.     printf ("Building Control Files for: '%s'\n", buf);
  120.     if ((cp = strstr(buf, ".")) != NULLCHAR)
  121.         *cp = 0;
  122.     ispublic = isarea (buf);
  123.     l.start = last = 0;
  124.     lines = 0;
  125.     while(fgets(buf,sizeof(buf),fp) != NULLCHAR){
  126.         if (!strncmp(buf, "From ", 5))    {
  127. #ifdef MSDOS
  128.             l.size = last - l.start - lines; 
  129. #else
  130.             l.size = last - l.start; 
  131. #endif
  132.             if (l.size)
  133.                 fwrite (&l, sizeof(struct let), 1, cfp);
  134.  
  135.             lines = l.status = 0;
  136.             l.start = last;
  137.             firstIDline = 0;
  138.         }
  139.         lines++;
  140.  
  141.         /* don't think this next section is necessary. All
  142.            messages SHOULD have a MSGID line */
  143.         if (!firstIDline && nextisBID && (cp=strstr(buf,"AA")) != NULLCHAR) {
  144.             /*what follows is the message-number*/
  145.             l.bid = atol(cp+2);
  146.             nextisBID = 0;
  147.             firstIDline = 1;
  148.             }
  149.  
  150.         if (!strncmp ("Received: ", buf, 10))
  151.             nextisBID = 1;
  152.         if (!ispublic && !strncmp ("Status: R", buf, 9))
  153.             l.status = BM_READ;
  154.         last = ftell (fp);
  155.     }
  156.     fclose(fp);
  157. #ifdef MSDOS
  158.     l.size = last - l.start - lines; 
  159. #else
  160.     l.size = last - l.start; 
  161. #endif
  162.     fwrite (&l, sizeof(struct let), 1, cfp);
  163.     fclose(cfp);
  164. }
  165. /*lint -restore */
  166.  
  167.  
  168. void
  169. main (int argc, char *argv[])
  170. {
  171. int k = 1; 
  172.  
  173.     if (argc == 1)    {
  174.         fprintf (stderr, "usage: buildctl [-ddirname] <file> [<file> ...]\n");
  175.         exit (1);
  176.     }
  177.     if (argv[1][0] == '-')    {
  178.         switch (tolower (argv[1][1]))    {
  179.             case 'd':    if (argv[1][2])        {
  180.                         dirname = &argv[1][2];
  181.                         k = 2;
  182.                         if (argc == 2)    {
  183.                             fprintf (stderr, "usage: buildctl [-ddirname] <file> [<file> ...]\n");
  184.                             exit (1);
  185.                         }
  186.                         break;
  187.                     }
  188.                     /* else fall through */
  189.             default:    printf ("usage: buildctl [-ddirname] [mailfile]\n");
  190.                     exit (1);
  191.         }
  192.     }
  193.     for ( ; k < argc; k++)        /*lint !e539 */
  194.         rebuild_one (argv[k]);
  195. }
  196.  
  197.